IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'SectionsGet')
	BEGIN
		DROP  Procedure  SectionsGet
	END

GO

CREATE Procedure SectionsGet
@ChapterID Int,
@CreatorID Int = Null
AS
If (@CreatorID Is Null)
Begin
	Select 
	SectionID,
	ChapterID,
	ImagePath,
	Body,
	AccessID,
	ViewOrder,
	CreatorID,
	CreatedDate
	From ChapterSection
	Where ChapterID = @ChapterID	Order By ViewOrder 
End

ELSE
BEGIN
Select * From (
	Select 
	SectionID,
	ChapterID,
	ImagePath,
	Body,
	AccessID,
	ViewOrder,
	CreatorID,
	CreatedDate
	From ChapterSection
	Where ChapterID = @ChapterID	And  IsPrivate = 0 
	Union All
Select 
	SectionID,
	ChapterID,
	ImagePath,
	Body,
	AccessID,
	ViewOrder,
	CreatorID,
	CreatedDate
	From ChapterSection
	Where ChapterID = @ChapterID	And  IsPrivate = 1 And CreatorID = @CreatorID 
	) AS Sections
	Order By ViewOrder ASC
END


GO



